#include "Scene.h"
#include "Node.h"
#include "UIText.h"
#include <algorithm>
using namespace std;

int Scene::sNodeNum = 0;

Scene::Scene(void)
{

}

Scene::~Scene(void)
{
}

Node* Scene::CreateNode( Node* parent )
{
	Node* node = new Node(sNodeNum++, parent);
	
	mNodes.push_back(node);

	return node;
}

Node* Scene::GetNodeByID( int id )
{
	NodeList::iterator it = mNodes.begin();
	while (it != mNodes.end())
	{
		Node* node = *it;
		if (node->GetID() == id)
		{
			return node;
		}
		it++;
	}
	
	return NULL;
}

UIText* Scene::GetTextByID( int id )
{
	TextList::iterator it = mUITexts.begin();
	while (it != mUITexts.end())
	{
		UIText* text = *it;
		if (text->GetID() == id)
		{
			return text;
		}
		it++;
	}

	return NULL;
}

void Scene::DeleteNode( Node* node )
{
	if (node == NULL)
	{
		return;
	}

	NodeList::iterator it = find(mNodes.begin(), mNodes.end(), node);

	if (it != mNodes.end())
	{
		mNodes.erase(it);
		
		Node::NodeList& children = node->GetChildNodes();

		it = children.begin();

		while (it != children.end())
		{
			this->DeleteNode(*it);
			it++;
		}

		delete node;
	}
	
}

UIText* Scene::CreateText( int x, int y, std::string text )
{
	UIText* t = new UIText(x, y, text);
	mUITexts.push_back(t);
	return t;
}
